<?php


class UploadUtil {
    public  $upload_name = null;
    private $upload_tmp_address  = null;
    public  $upload_filetype  = null;
    private $file_server_address  = null;
    public  $save_name  = null;
    public  $upload_file_size  = null;
    private $upload_must_size = 10000000; //允许上传文件的大小，自己设置
    private $_fieldName =   null;
    public  $_save_folder = null;
    public  $upload_files = array();

    public static function  is_files_upload($field){
        $uploadFiles= $_FILES[$field]["tmp_name"];
        if(empty($uploadFiles)) {
            return false;
        }
        if(is_array($uploadFiles) && count($uploadFiles)==0){
            return false;        
        }
        return true;
    }


    private static function mkFolder($path)  {  
        if(!is_readable($path))  {  
            is_file($path) or mkdir($path,0700);  
        }  
    } 


    public function __construct($fieldName=null,$uppath=null){
        $this->_fieldName=$fieldName;
        $this->_save_folder=$uppath;
        UploadUtil::mkFolder($uppath);

    }

    function uploadFiles($saveName=null,$uppath =null,$fieldName=null) {
        $uploadFiles= $_FILES[$this->_fieldName]["tmp_name"];
         if(is_array($uploadFiles) && count($uploadFiles)>=0){
            
             $this->uploadMultFiles($uppath ,$fieldName);  
         }
         else{
           
             $this->uploadSingleFile($saveName,$uppath ,$fieldName); 
         }
         return $this->upload_files;
    }

    function uploadSingleFile($saveName=null,$uppath =null,$fieldName=null) {

            $fileInfo = array();
            if($fieldName!=null && $fieldName!=""){
                $this->_fieldName=$fieldName;
            }
            if($uppath!=null && $uppath!=""){
                $this->_save_folder=$uppath;
            }
            if($saveName!=null && $saveName!=""){
                $this->save_name=$saveName;
            }


            $this->upload_filetype = $_FILES[$this->_fieldName]["type"];
            
            $this->upload_tmp_address = $_FILES[$this->_fieldName]["tmp_name"]; //取得临时地址


            $this->upload_name = $_FILES[$this->_fieldName]["name"];
            $arr = explode(".",$this->upload_name);
            if($this->save_name == null){
                 $this->save_name = Utils::guid().".".$arr[1];   
            }

            $this->upload_file_size = $_FILES[$this->_fieldName]["size"]; //上传文件的大小
            $returnvalue = null;
            if ($this->upload_file_size < $this->upload_must_size) {
                $this->file_server_address = $this->_save_folder . $this->save_name;

                move_uploaded_file($this->upload_tmp_address, $this->file_server_address); //从TEMP目录移出
                $returnvalue = $this->save_name;
            } else {
                $returnvalue = 0;
            }

           /* $fileInfo->upload_name = $_FILES[$this->_fieldName]["name"];
            $fileInfo->save_name = $this->save_name ;
            $fileInfo->file_size = $_FILES[$this->_fieldName]["size"];
            $fileInfo->file_type = $_FILES[$this->_fieldName]["type"];
            $fileInfo->file_path = $this->file_server_address;*/

            $fileInfo["upload_name"] =$_FILES[$this->_fieldName]["name"];
            $fileInfo["save_name"] =  $this->save_name ;
            $fileInfo["file_size"] = $_FILES[$this->_fieldName]["size"];
            $fileInfo["file_type"] = $_FILES[$this->_fieldName]["type"];
            $fileInfo["file_path"] = $this->file_server_address;


            array_push($this->upload_files, $fileInfo);
}
    

    function uploadMultFiles($uppath =null,$fieldName=null) {
        $uploadFiles= $_FILES[$this->_fieldName]["tmp_name"];
        for($i = 0;$i<count($uploadFiles);$i++){
            $this->uploadFileByIndex($i,$uppath,$fieldName);
        }    
    }

    function uploadFileByIndex($index,$uppath =null,$fieldName=null) {
            $fileInfo = array();
            $uploadFiles= $_FILES[$this->_fieldName]["tmp_name"];
            if(!is_array($uploadFiles) || $index>=count($uploadFiles) ){
                return;
            }

            if(!is_uploaded_file($uploadFiles[$index])){
                return ;
            }


            if($fieldName!=null && $fieldName!=""){
                $this->_fieldName=$fieldName;
            }
            if($uppath!=null && $uppath!=""){
                $this->_save_folder=$uppath;
            }

            /*if($saveName!=null && $saveName!=""){
                $this->save_name=$saveName;
            }*/


            $this->upload_filetype = $_FILES[$this->_fieldName]["type"][$index];
            
            $this->upload_tmp_address = $uploadFiles[$index]; //取得临时地址



            $this->upload_name = $_FILES[$this->_fieldName]["name"][$index];
            $arr = explode(".",$this->upload_name);
            $saveName = Utils::guid().".".$arr[1];   
            $this->upload_file_size = $_FILES[$fieldName]["size"][$index]; //上传文件的大小
            $returnvalue = null;
            if ($this->upload_file_size < $this->upload_must_size) {
                $this->file_server_address = $this->_save_folder . $saveName;
                move_uploaded_file($this->upload_tmp_address, $this->file_server_address); //从TEMP目录移出
                
                $fileInfo["upload_name"] = $_FILES[$this->_fieldName]["name"][$index];
                $fileInfo["save_name"] = $saveName ;
                $fileInfo["file_size"] = $_FILES[$fieldName]["size"][$index];
                $fileInfo["file_type"] = $_FILES[$this->_fieldName]["type"][$index];
                $fileInfo["file_path"] = $this->file_server_address;
                
                array_push($this->upload_files, $fileInfo);
            }
        }
    
}
?>
